<?php
class Admin_GhotController extends Zend_Controller_Action
{
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	protected $_page =1;
	
	protected $_name = '';
	
	protected $_cat = 0;
	//Duong dan cua Action chinh
	
	protected $_actionMain;
	
	public function init()
	{
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		$this->_page = $this->_arrParam['page'];
		$this->_name = $this->_arrParam['name'];
		$this->_cat = $this->_arrParam['parent'];
		if($this->_page=='' || $this->_page <=0){
			$this->_page =1;
		}
		
		if($this->_cat=='' || $this->_cat <=0){
			$this->_cat =0;
		}
		//Duong dan cua Action chinh
		if($this->_cat){
			$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index/page/'.$this->_page.'/cat/'.$this->_cat;
		}else{
			$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index/page/'.$this->_page;
		}
		
		if($this->_name){
			$this->_actionMain .='/name/'.$this->_name;
		}
	}
	
	public function indexAction()
	{
		$translate = Zend_Registry::get('Zend_Translate');
		$this->view->title = $translate->_("TITLE_ADMIN_GAMES_INDEX_HOT");
		$this->view->headTitle($this->view->title);
		
		$menuModel = new Admin_Model_Menu();
		$listMenu = $menuModel->fetchMenu(array('parent'=>''));
		$listMenu = $listMenu->toArray();		
		$optionsMenu = array('0'=>'Tất cả');		
		foreach ($listMenu as $key => $value){
			$optionsMenu[$value['id']] = $value['name'];
			$listSub = $menuModel->fetchMenu(array('parent'=>$value['id']));
			$listSub = $listSub->toArray();
			foreach ($listSub as $keySub => $valueSub){
				$optionsMenu[$valueSub['id']] = '--'.$valueSub['name'];
			}
		}
		$this->view->filter = $this->_name;
		$this->view->cat = $this->_cat;			
		$gamesUser = new Admin_Model_Games();
		if($this->_cat > 0){
			$listGames = $gamesUser->searchGames($this->_cat,$this->_name, 1);
		}else{
			if($this->_name){
				$listGames = $gamesUser->fetchGames(array('name'=>$this->_name,'game_hot'=>1));
			}else{
				$listGames = $gamesUser->fetchGames(array('game_hot'=>1));
			}
		}
		
		$paginator = Zend_Paginator::factory($listGames);
		$paginator->setItemCountPerPage(NUM_PER_PAGE);
	    $paginator->setCurrentPageNumber($this->_page);
	    $this->view->page = $this->_page;
	    $this->view->paginator = $paginator;
	    $this->view->select_cat = $optionsMenu;  
	}	
	
	public function activeAction(){
		$this->_helper->layout()->disableLayout();
		$type = $this->_request->getParam('type',1);
		$id = $this->_request->getParam('id',0);
		if($type>0){
			$type = 1;
		}else{
			$type = 0;
		}
		
		$modelGames = new Admin_Model_Games();
		$modelGames->update(array('active'=>$type), 'id='.$id);
		$this->_redirect($this->_actionMain);
	}
	
	public function deleteAction(){
		$this->_helper->layout()->disableLayout();
		$id = $this->_request->getParam('id',0);		
		$modelGames = new Admin_Model_Games();
		$modelGames->update(array('game_hot'=>0), 'id='.$id);
		$this->_redirect($this->_actionMain);
	}
	
	public function deleteitemsAction(){
		$this->_helper->layout()->disableLayout();
		$gameModel = new Admin_Model_Games();
		$item = $this->_arrParam['cid'];
		foreach ($item as $k=>$v){
			$gameModel->update(array('game_hot'=>0), 'id='.$v);
		}
		$this->_redirect($this->_actionMain);
	}
}